package com.pay.game.central.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.uuid.Generators;
import com.pay.game.central.bot.MyBot;
import com.pay.game.central.common.Result;
import com.pay.game.central.common.ResultGenerator;
import com.pay.game.central.security.annotaion.PassToken;
import com.pay.game.central.web.bean.TelegramBill;
import com.pay.game.central.web.bean.TelegramCard;
import com.pay.game.central.web.bean.TelegramGroup;
import com.pay.game.central.web.service.BotService;
import com.pay.game.central.web.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RequestMapping("/bot")
@Controller
public class BotController {

    @Autowired
    private MyBot mMyBot;

    @Autowired
    private DeviceService mDeviceService;

    @Autowired
    private BotService mBotService;

    @PostMapping("/bill/upload")
    @ResponseBody
    Result<String> uploadBill(@RequestBody String body) {
        JSONObject jo = JSON.parseObject(body);
        String realName = jo.getString("realName");
        String name = jo.getString("name");
        int value = jo.getInteger("value");
        int time = jo.getInteger("time");
        TelegramCard telegramCard = mBotService.getCardInfo(realName);
        mMyBot.sendMsg(telegramCard.getTelegram_group_id(), name);
        mMyBot.sendMsg(telegramCard.getTelegram_group_id(), String.format("%.2f", new Float(value) / 100.0));
        TelegramBill bill = new TelegramBill();
        bill.setCard_id(telegramCard.getId());
        bill.setCard_real_name(telegramCard.getReal_name());
        bill.setReal_name(name);
        bill.setValue(value);
        bill.setTime(time);
        bill.setId(Generators.timeBasedGenerator().generate().toString());
        mBotService.addTelegramBill(bill);
        return ResultGenerator.genSuccessResult("ok");
    }

    @GetMapping("/bill/list")
    @ResponseBody
    Result<List<TelegramBill>> listBill(){
        return ResultGenerator.genSuccessResult(mBotService.listTelegramBill());
    }

    @GetMapping("/card/online")
    @ResponseBody
    Result<String> cardOnline(String realName){
        mDeviceService.updateOnlineTime(realName);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/group/add")
    @ResponseBody
    Result<String> addGroup(@RequestBody String body){
        JSONObject jo = JSON.parseObject(body);
        TelegramGroup tgg = new TelegramGroup();
        tgg.setGroup_comment(jo.getString("comment"));
        tgg.setGroup_name(jo.getString("name"));
        tgg.setId(jo.getString("id"));
        mBotService.addTelegramGroup(tgg);
        return ResultGenerator.genSuccessResult("ok");
    }

    @GetMapping("/group/list")
    @ResponseBody
    Result<List<TelegramGroup>> listGroup(){
        return ResultGenerator.genSuccessResult(mBotService.listGroup());
    }

    @PostMapping("/group/delete")
    @ResponseBody
    Result<String> deleteGroup(@RequestBody String body){
        JSONObject jo = JSON.parseObject(body);
        mBotService.deleteTelegramGroup(jo.getString("id"));
        return ResultGenerator.genSuccessResult("ok");
    }


    @PostMapping("/card/add")
    @ResponseBody
    Result<String> addCard(@RequestBody String body){
        JSONObject jo = JSON.parseObject(body);
        TelegramCard tgc = new TelegramCard();
        tgc.setReal_name(jo.getString("real_name"));
        tgc.setId(jo.getString("id"));
        tgc.setTelegram_group_id(jo.getString("telegram_group_id"));
        mBotService.addTelegramCard(tgc);
        return ResultGenerator.genSuccessResult("ok");
    }

    @GetMapping("/card/list")
    @ResponseBody
    Result<List<TelegramCard>> listCard(){
        return ResultGenerator.genSuccessResult(mBotService.listTelegramCard());
    }

    @ResponseBody
    @GetMapping("/card/listOnline")
    Result<Set<String>> listOnlineCard(){
        return ResultGenerator.genSuccessResult(mDeviceService.getOnlineDevice());
    }

    @PostMapping("/card/disable")
    @ResponseBody
    Result<String> disableCard(@RequestBody String body){
        JSONObject jo = JSON.parseObject(body);
        String id = jo.getString("id");
        mBotService.disableCard(id);
        return ResultGenerator.genSuccessResult("ok");
    }

    @PassToken
    @PostMapping("/card/bind")
    @ResponseBody
    Result<String> bindCard(@RequestBody String body){
        JSONObject jo = JSON.parseObject(body);
        String realName = jo.getString("realName");
        String deviceId = jo.getString("deviceId");
//        -1 找不到卡，0 卡已经禁用，1 卡已经绑定， 2 卡未绑定
        int status = mBotService.isCardBind(realName, deviceId);
        if (status == -1) return ResultGenerator.genFailResult("找不到卡, 请确认该储蓄卡是否录入系统。");
        if (status == 0) return ResultGenerator.genFailResult("卡已经禁用");
        if (status == 1) return ResultGenerator.genFailResult("卡已经绑定");
        if (status == 3)  return ResultGenerator.genSuccessResult("ok");
        mBotService.bindCard(realName, deviceId);
        return ResultGenerator.genSuccessResult("ok");
    }

    @PostMapping("/card/unbind")
    @ResponseBody
    Result<String> unbindCard(@RequestBody String body){
        JSONObject jo = JSON.parseObject(body);
        String id = jo.getString("id");
        mBotService.unbindCard(id);
        return ResultGenerator.genSuccessResult("ok");
    }

}
